<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        :before-open="beforeOpen"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel">
        <template slot="menuLeft">
         <!-- <el-button
            v-if="permissions.workorder_shiftmanage_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">导出
          </el-button>-->
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text"  icon="el-icon-edit"  size="small" plain  @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text"  icon="el-icon-view"  size="small" plain  @click="personInfoOpen(scope.row)">人员信息</el-button>
          <el-button type="text"  icon="el-icon-delete"  size="small" plain  @click="handleDel(scope.row,scope.index)">删除</el-button>
        </template>
、

        <template slot-scope="scope" slot="restDurationForm">
          <div class="block">
            <avue-input-number  v-model="restDuration" @input="restDurationChange"  :min="0" :controls=false style="width:90%!important" /> 小时
          </div>
        </template>
      </avue-crud>

      <!--人员信息弹窗页面-->
      <el-dialog title=""
                 :visible.sync="personDialogVisible"
                 width="50%"
                 lock-scroll
                 v-if="personDialogVisible"
                 :fullscreen="personDialogFull">
        <el-backtop target=".el-card__body .el-dialog__wrapper .el-dialog" :right="15" :bottom="20" :visibility-height="1" />
        <el-backtop :bottom="60"></el-backtop>
        <template slot="title">
          <div class="avue-crud__dialog__header">
            <span class="el-dialog__title">
                <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
               归属产线：{{productLineName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>班次：{{shiftName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>人员信息
            </span>
          </div>
        </template>
        <!--人员信息页面-->
        <personInfo ref="personRef"  :shiftIdStr="shiftId" :shiftNameStr="shiftName" :productLineNameStr="productLineName"  :productLineIdStr="productLineId" />
      </el-dialog>


    </basic-container>
  </div>
</template>

<script>
import { getLineList} from '@/api/pfoc/realityproductlinemain' //获取产线集合
import { fetchList, addObj, putObj, delObj } from '@/api/workorder/shiftmanage'
import { tableOption } from '@/const/crud/workorder/shiftmanage'
import { mapGetters } from 'vuex'
/**引入班次人员岗位信息页面*/
import personInfo from '@/views/workorder/shiftmanage/personInfo'

export default {
  name: 'shiftmanage',
  components: {
      //人员信息页面
      personInfo,
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      lineList:[],//产线集合,
      personDialogVisible:false,//人员信息页面开关
      personDialogFull:true,//人员信息页面弹窗缩放开关（默认放大）
      shiftId:null,//存储班次ID
      shiftName:null,//班次名称
      productLineName:null,//产线名称
      productLineId:null,//产线ID
      restDuration:undefined,//休息时长



    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.workorder_shiftmanage_add, false),
        delBtn: this.vaildData(this.permissions.workorder_shiftmanage_del, false),
        editBtn: this.vaildData(this.permissions.workorder_shiftmanage_edit, false)
      }
    }
  },
  created() {
      //获取产线集合
      this.getLineListData();
  },
  methods: {
    // 列表查询
    getList(page, params) {
        console.log(this.searchForm)
      this.tableLoading = true
      fetchList(Object.assign({
        descs:"sort_order",
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除班次【' + row.shiftName+'】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function(row, index, done, loading) {
      row.restDuration = this.restDuration;
      this.lineList.forEach(function (item, index) {
          if(item.id == row.productLineId){
              row.productLineName = item.realityLineName;
          }
      });
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function(row, done, loading) {
      row.restDuration = this.restDuration;
      this.lineList.forEach(function (item, index) {
          if(item.id == row.productLineId){
              row.productLineName = item.realityLineName;
          }
      });
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/workorder/shiftmanage/export', this.searchForm, 'shiftmanage.xlsx')
    },
    //获取产线集合
    getLineListData(){
        getLineList().then(response => {
            this.lineList = response.data.data;
            console.log(response)
        })
    },
    //打开人员信息窗口
    personInfoOpen(row){
        this.personDialogVisible=true;//人员信息页面开关
        this.shiftId=row.id;//存储班次ID
        this.shiftName=row.shiftName;//班次名称
        this.productLineName=row.productLineName;//产线名称
        this.productLineId=row.productLineId;//产线ID
        this.$nextTick(() => {
            //调用人员岗位信息查询方法
            this.$refs.personRef.refreshChange();
        })
    },
    //表单打开之前清空重置
    beforeOpen(done,type){
        if (["add"].includes(type)) {
            this.$nextTick(() => {
                var form =  this.$refs['crud'].tableForm;
                Object.keys(form).forEach((key) =>{
                    form[key] = null;
                })
            })
            this.restDuration = undefined;
        }
        done();
    },
    //默认编辑方法
    handleEdit:function(row,index){
        this.restDuration=row.restDuration;//休息时长
        this.$refs.crud.rowEdit(row, index);
    },
    //时长填写
    restDurationChange(){
        //时长
        var  restDuration = this.findObject(this.tableOption.column, 'restDuration');
        this.$set(this.$refs['crud'].tableForm, 'restDuration', this.restDuration);
    }
  }
}
</script>
